package com.ruoyi.activiti.controller;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.activiti.domain.BookBorrow;
import com.ruoyi.activiti.service.IBizProcessInstanceRelationService;
import com.ruoyi.activiti.service.IProcessService;
import com.ruoyi.activiti.service.impl.BookBorrowServiceExtImpl;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * 书籍借阅Controller
 * 
 * @author wushi
 * @date 2024-11-19
 */
@Controller
@RequestMapping("/bisiness/borrow")
public class BookBorrowController extends BaseController
{
    private String prefix = "bisiness/borrow";

    @Autowired
    private BookBorrowServiceExtImpl bookBorrowService;

    @Autowired
    private TaskService taskService;
    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private IBizProcessInstanceRelationService bizProcessInstanceRelationService;

    @Autowired
    private IProcessService processService;

    @RequiresPermissions("bisiness:borrow:view")
    @GetMapping()
    public String borrow(ModelMap mmap)
    {
        mmap.put("currentUser", ShiroUtils.getSysUser());
        return prefix + "/borrow";
    }

    /**
     * 查询书籍借阅列表
     */
    @RequiresPermissions("bisiness:borrow:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BookBorrow bookBorrow)
    {
        startPage();
        List<BookBorrow> list = bookBorrowService.selectBookBorrowList(bookBorrow);
        return getDataTable(list);
    }

    /**
     * 导出书籍借阅列表
     */
    @RequiresPermissions("bisiness:borrow:export")
    @Log(title = "书籍借阅", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BookBorrow bookBorrow)
    {
        List<BookBorrow> list = bookBorrowService.selectBookBorrowList(bookBorrow);
        ExcelUtil<BookBorrow> util = new ExcelUtil<BookBorrow>(BookBorrow.class);
        return util.exportExcel(list, "borrow");
    }

    /**
     * 新增书籍借阅
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存书籍借阅
     */
    @RequiresPermissions("bisiness:borrow:add")
    @Log(title = "书籍借阅", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BookBorrow bookBorrow)
    {
        return toAjax(bookBorrowService.insertBookBorrow(bookBorrow));
    }

    /**
     * 修改书籍借阅
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BookBorrow bookBorrow = bookBorrowService.selectBookBorrowById(id);
        mmap.put("bookBorrow", bookBorrow);
        return prefix + "/edit";
    }

    /**
     * 修改保存书籍借阅
     */
    @RequiresPermissions("bisiness:borrow:edit")
    @Log(title = "书籍借阅", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BookBorrow bookBorrow)
    {
        return toAjax(bookBorrowService.updateBookBorrow(bookBorrow));
    }

    /**
     * 删除书籍借阅
     */
    @RequiresPermissions("bisiness:borrow:remove")
    @Log(title = "书籍借阅", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bookBorrowService.deleteBookBorrowByIds(ids));
    }

    @Log(title = "书籍借阅", businessType = BusinessType.APPLY)
    @PostMapping( "/submitApply")
    @ResponseBody
    public AjaxResult submitApply(Long id) {
        BookBorrow bookBorrow = bookBorrowService.selectBookBorrowById(id);
        String applyUserId = ShiroUtils.getLoginName();
        bookBorrowService.submitApply(bookBorrow, applyUserId, "bisiness/borrow", new HashMap<>());
        return success();
    }

    /****************************************************************
     * 加载审批弹窗
     * @param taskId
     * @param mmap
     * @return
     */
    @GetMapping("/showVerifyDialog/{taskId}")
    public String showVerifyDialog(@PathVariable("taskId") String taskId, ModelMap mmap) {
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        String processInstanceId = task.getProcessInstanceId();
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        BookBorrow bookBorrow = bookBorrowService.selectBookBorrowById(new Long(processInstance.getBusinessKey()));
        mmap.put("bookBorrow", bookBorrow);
        mmap.put("taskId", taskId);
        mmap.put("isEditable", false);

        return prefix + "/approval";
    }

    @GetMapping("/showFormDialog/{instanceId}")
    public String showFormDialog(@PathVariable("instanceId") String instanceId, ModelMap mmap) {
        String businessKey = processService.findBusinessKeyByInstanceId(instanceId);
        BookBorrow bookBorrow = bookBorrowService.selectBookBorrowById(new Long(businessKey));
        mmap.put("bookBorrow", bookBorrow);
        mmap.put("isEditable", false);
        return prefix + "/view";
    }
}
